<template>
  <div class="platformLogin">
    <CustomForm
      v-if="role === 'youke' || role === ''"
      ref="CustomForm"
      :formRootInstance="formRootInstance"
      :formRules="formRules"
      :initModel="initModel"
      :tempModel="tempModel"
      :submitButtonOptions="submitButtonOptions"
      :curCaptchaPath="curCaptchaPath"
      @refreshAuthCode="getCaptchaPath"
      @submit="handleSubmitForm"
      @register="handleRegister"
    >
      <template v-slot:footer>
        <a-row>
          <a-col :span="8" :offset="18">
            <a class="a-tag-login" @click="handleForget">忘记密码</a>
          </a-col>
        </a-row>
      </template>
    </CustomForm>
    <div class="welcome" v-else>
      <span class="name">欢迎你，{{ userName }}</span>
      <div class="calendar">
        <div class="content">
          <div class="content-left">
            <span class="year">{{ year }}年</span>
            <div class="month">
              <span class="num">{{ month }}</span>
              <span class="font">月</span>
            </div>
          </div>
          <span class="content-middle">{{ date }}</span>
          <span class="content-right">{{ week }}</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
// import qs from 'qs'
import { mapGetters, mapMutations } from 'vuex'
import encrypt from '@/utils/cryptoJs.js'
import CustomForm from '@/components/CustomForm/index'
import mixinConfigSetting from '@/mixins/mixinConfigSetting'
import mixinLogin from '@/views/Login/mixins/index'
// import { checkToken, emailToken } from '@/api/user'
import { MODAL_FOOTER_CONFIG } from '@/views/Login/config/modal'
import { captchaImage } from '@/api/user'
// import { captchaImage, Login } from '@/api/user'
// import { setStorage } from '@/utils/index'

export default {
  mixins: [mixinConfigSetting, mixinLogin],
  components: {
    CustomForm,
  },
  props: {
    formConfig: Object // 表单配置对象(在mixinConfigSetting 中统一处理了)
  },
  data() {
    return {
      btnList: MODAL_FOOTER_CONFIG.btnList,
      uuid: '',
      year: 0,
      month: 0,
      date: 0,
      week: ''
    }
  },
  mounted() {
    this.normalizeDate()
    // 验证码
    this.getCaptchaImage()
  },
  computed: {
    ...mapGetters('user', [
      'role',
      'account',
      'visitor',
      'token',
      'userName'
    ])
  },
  methods: {
    ...mapMutations('user', [
      'SET_ACCOUNT',
      'SET_ROLE',
      'SET_TOKEN'
    ]),
    normalizeDate() {
      const nowdate = new Date();
      const week = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"]
      this.year = nowdate.getFullYear()
      this.month = nowdate.getMonth() + 1,
        this.date = nowdate.getDate()
      const day = nowdate.getDay()
      this.week = week[day]
    },
    getCaptchaImage() {
      captchaImage().then((res) => {
        if (res.code === 200) {
          this.curCaptchaPath = `data:image/gif;base64,${res.img}`
          this.uuid = res.uuid
        }
      })
    },
    getCaptchaPath() {
      this.getCaptchaImage()
    },
    handleSubmitForm(formValue) {
      this.callWithHandlerError(async () => {
        const { form } = formValue
        this.normalizeLoginForm(form) // 格式化表单
        this.$store.dispatch('user/LoginForm', form).then((res) => {
          if (res.code === 200) {
            this.$message.success('登录成功!')
            // this.$refs.CustomForm.resetForm()
            this.emailLogin() // 执行登录逻辑
          } else {
            this.getCaptchaPath() // 重新获取验证码
            this.$message.warning(res.msg)
          }
        })
      })
    },
    handleRegister() {
      this.$router.push({
        path: '/login',
        query: { toComponent: 'register' },
      })
    },
    handleForget() {
      this.$router.push({
        path: '/login',
        query: { toComponent: 'forget' },
      })
    },
    normalizeLoginForm(form) { // 标准化参数
      const { password, email } = form
      this.loginForm = {
        client_id: window.APP_CONFIG.loginData.client_id,
        client_secret: window.APP_CONFIG.loginData.client_secret,
        grant_type: 'password',
        email,
        password: encrypt(password, this.uuid),
        code: form.code,
        uuid: this.uuid,
        crowd_out: false,
        scope: 'select'
      }
      form.uuid = this.uuid
      // this.checkToken(email)
    },
    async emailLogin() {
      //由于首页没有权限要求，可以不刷新页面获取最新token
      // window.location.replace('/') //首页有token权限要求则需刷新页面
      // 处理滚动条回滚到顶部
      const domWrp = document.getElementsByClassName('home-wrap')
      if (domWrp.length > 0) {
        domWrp[0].scrollTop = 0
      }
    }
  }
}
</script>